package com.yangjun.controller.realtimeLiveAnalysis.realtimeSource;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.yangjun.common.DVConstants;
import com.yangjun.dao.impl.realtimeLiveAnalysis.realtimeSource.AndroidIOSImpl;
import com.yangjun.dao.mybatis.mapper.realtimeLiveAnalysis.AndroidIosInfoMapper;
import com.yangjun.entity.realtimeliveAnalysis.AndroidIosLiveAreaEntity;
import com.yangjun.utils.DvDateUtils;
import com.yangjun.utils.EchartUtils;

@Controller
@RequestMapping("/realtimesource/android")
public class AndroidController {
	
	private static final Integer PLATFORM=14;
	
	
	@Autowired
	AndroidIOSImpl mModel;
	
    @Autowired
	private AndroidIosInfoMapper mapper;
	
    @RequestMapping("/overview")
    @ResponseBody
    public Map<String,Object>  overview(HttpServletResponse response,
			@RequestParam(value = "today", required = false) String today,
			@RequestParam(value = "contrastday", required = false) String contrastday,
			@RequestParam(value = "radioname", required = false) String radioname) {
    	
    	Map<String, Object> resultMap = Maps.newHashMap();
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	response.setCharacterEncoding("UTF-8");
    	response.setContentType("text/html;charset=UTF-8");
    	
    	// 参数异常处理
    	if (StringUtils.isBlank(today))
    		today = DvDateUtils.getDate(new Date(), 0);
    	if (StringUtils.isBlank(contrastday))
    		contrastday = DvDateUtils.getDate(new Date(), -7);
    	
    	String yestoday = DvDateUtils.getDate(new Date(), -1);
    	
    	
    	/**
    	 * 概况
    	 */
    	 
    	List<Map<String, Object>> overviewDataList = mModel.getOverviewData(today,yestoday, contrastday,PLATFORM);
    	resultMap = EchartUtils.resultMap(today, yestoday, contrastday, overviewDataList);
    	

    	/**
    	 * 新增用户 应用版本
    	 */
    	
    	Map<String,Object> newClientVersionMap = Maps.newHashMap();
    	List<Object> newClientVersionLegend=new ArrayList<Object>();
    	List<Map<String, Object>> newClientVersionDate = new ArrayList<Map<String, Object>>();
    	
    	List<Map<String, Object>> newClientVersion = mModel.getNewClientVersion(PLATFORM);
    	for(Map<String, Object> val : newClientVersion){
    		Map<String, Object> date = new HashMap<String, Object>();
    		
    		Object key = val.get("VERSIONNAME");
    		Object value = val.get("NEW_CLIENT");
    		date.put("name", key);
    		date.put("value", value);
    		newClientVersionLegend.add(key);
    		newClientVersionDate.add(date);
    		
    		
    	}
    	newClientVersionMap.put("legend", newClientVersionLegend);
    	newClientVersionMap.put("data", newClientVersionDate);
    	resultMap.put("newClientVersion", newClientVersionMap);
    	
    	/**
    	 * 新增用户 渠道
    	 */
    	
/*    	Map<String,Object> newClientUtmSourceMap = Maps.newHashMap();
    	List<Object> newClientUtmSourceLegend=new ArrayList<Object>();
    	List<Map<String, Object>> newClientUtmSourceDate = new ArrayList<Map<String, Object>>();
    	
    	List<Map<String, Object>> newClientUtmSource = mModel.getNewClientUtmSource(PLATFORM);
    	for(Map<String, Object> val : newClientUtmSource){
    		Map<String, Object> date = new HashMap<String, Object>();
    		
    		Object key = val.get("utm_source");
    		Object value = val.get("new_client");
    		date.put("name", key);
    		date.put("value", value);
    		newClientUtmSourceLegend.add(key);
    		newClientUtmSourceDate.add(date);
    	}
    	newClientUtmSourceMap.put("legend", newClientUtmSourceLegend);
    	newClientUtmSourceMap.put("data", newClientUtmSourceDate);
    	resultMap.put("newClientUtmSource", newClientUtmSourceMap);*/
    	
    	/**
    	 * 活跃用户 应用版本
    	 */
    	
    	Map<String,Object> appVersionMap = Maps.newHashMap();
    	List<Object> appVersionLegend=new ArrayList<Object>();
    	List<Map<String, Object>> appVersionDate = new ArrayList<Map<String, Object>>();
    	
    	List<Map<String, Object>> appVersion = mModel.getAppVersion(PLATFORM);
    	for(Map<String, Object> val : appVersion){
    		Map<String, Object> date = new HashMap<String, Object>();
    		
    		Object key = val.get("versionname");
    		Object value = val.get("client_number");
    		date.put("name", key);
    		date.put("value", value);
    		appVersionLegend.add(key);
    		appVersionDate.add(date);
    		
    		
    	}
    	appVersionMap.put("legend", appVersionLegend);
    	appVersionMap.put("data", appVersionDate);
    	resultMap.put("appVersion", appVersionMap);
    	
    	
    	/**
    	 * 活跃用户 渠道
    	 */
    	
/*    	Map<String,Object> appUtmSourceMap = Maps.newHashMap();
    	List<Object> appUtmSourceLegend=new ArrayList<Object>();
    	List<Map<String, Object>> appUtmSourceDate = new ArrayList<Map<String, Object>>();
    	List<Map<String, Object>> appUtmSource = mModel.getAppUtmSource(PLATFORM);
    	for(Map<String, Object> val : appUtmSource){
    		Map<String, Object> date = new HashMap<String, Object>();
    		Object key = val.get("utm_source");
    		Object value = val.get("client_number");
    		date.put("name", key);
    		date.put("value", value);
    		appUtmSourceLegend.add(key);
    		appUtmSourceDate.add(date);
    	}
    	appUtmSourceMap.put("legend", appUtmSourceLegend);
    	appUtmSourceMap.put("data", appUtmSourceDate);
    	resultMap.put("appUtmSource", appUtmSourceMap);*/
    	
    	
    	return resultMap;
    }
    
    
    /**
     * 新用户安装渠道
     * @param response
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/newsource")
    @ResponseBody
    public PageInfo<AndroidIosLiveAreaEntity> getNewsource(HttpServletResponse response,
    		@RequestParam(value = "pageNum",required = true)String pageNum,
    		@RequestParam(value = "pageSize",required = true)String pageSize){
    	
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	response.setCharacterEncoding("UTF-8");
    	response.setContentType("text/html;charset=UTF-8");
    	
    	PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize),true,true,true);
    	PageInfo<AndroidIosLiveAreaEntity> page = new PageInfo<AndroidIosLiveAreaEntity>(mapper.selectNewSource());
    	return page;
    }
    
    /**
     * 活跃用户安装渠道
     * @param response
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/activesource")
    @ResponseBody
    public PageInfo<AndroidIosLiveAreaEntity> getActivesource(HttpServletResponse response,
    		@RequestParam(value = "pageNum",required = true)String pageNum,
    		@RequestParam(value = "pageSize",required = true)String pageSize){
    	
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	response.setCharacterEncoding("UTF-8");
    	response.setContentType("text/html;charset=UTF-8");
    	
    	PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize),true,true,true);
    	PageInfo<AndroidIosLiveAreaEntity> page = new PageInfo<AndroidIosLiveAreaEntity>(mapper.selectActiveSource());
    	return page;
    }
    
    
    
    
    
    
    /**
     * 地域分布
     * @param response
     * @param pageNum
     * @param pageSize
     * @return
     */
    
    @RequestMapping("/area")
    @ResponseBody
    public PageInfo<AndroidIosLiveAreaEntity> getArea(HttpServletResponse response,
    		@RequestParam(value = "pageNum",required = true)String pageNum,
    		@RequestParam(value = "pageSize",required = true)String pageSize){
    	
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	response.setCharacterEncoding("UTF-8");
    	response.setContentType("text/html;charset=UTF-8");
    	
    	PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize),true,true,true);
    	PageInfo<AndroidIosLiveAreaEntity> page = new PageInfo<AndroidIosLiveAreaEntity>(mapper.select(PLATFORM));
    	return page;
    }
    
    
    /**
     * 
     * http://192.168.7.32:8080/realtimesource/android/radioLine?today=2016-11-07&contrastday=2016-11-06
     * 单选按钮 折线图
     * @param response
     * @param today
     * @param contrastday
     * @return
     */
    @RequestMapping("/radioLine")
    @ResponseBody
    public Map<String,Object>  radioLine(HttpServletResponse response,
			@RequestParam(value = "today", required = true) String today,
			@RequestParam(value = "contrastday", required = true) String contrastday) {
    	Map<String, Object> resultMap = Maps.newHashMap();
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	response.setCharacterEncoding("UTF-8");
    	response.setContentType("text/html;charset=UTF-8");
    	
//    	System.err.println("today--"+today);
//    	System.err.println("contrastday--"+contrastday);
    	
    	// 参数异常处理
    	if (StringUtils.isBlank(today))
    		today = DvDateUtils.getDate(new Date(), 0);
    	if (StringUtils.isBlank(contrastday))
    		contrastday = DvDateUtils.getDate(new Date(), -7);
    	
//    	System.err.println("contrastday--"+contrastday);
//    	System.err.println("today--"+today);
    	
    	/**
    	 * 单选按钮
    	 */
    	
    	Map<String, List<Map<String, Object>>> radioLine = mModel.getOverviewLine(PLATFORM,today, contrastday);
    	for (Map.Entry<String, List<Map<String, Object>>> entry : radioLine.entrySet()) {  
    	    String key = entry.getKey();
    	    List<Map<String, Object>> value = entry.getValue();
//			System.out.println("Key = " + key + ", Value = " + value);
			if("startUprows".equals(key)){
				//启动app
		    	List<Map<String, Object>> startUprows=radioLine.get("startUprows");
		    	resultMap.put("startUprows",getRadioLine(today,contrastday,"OPEN_CLIENT_NUMBER",startUprows));
		    	
			}else if("newClientrows".equals(key)){
				//新用户
				List<Map<String, Object>> startUprows=radioLine.get("newClientrows");
		    	resultMap.put("newClientrows",getRadioLine(today,contrastday,"NEW_CLIENT",startUprows));
				
			}else if("oldClientrows".equals(key)){
				//老用户
				List<Map<String, Object>> startUprows=radioLine.get("oldClientrows");
		    	resultMap.put("oldClientrows",getRadioLine(today,contrastday,"OLD_CLIENT",startUprows));
				
			}else if("newclientRaterows".equals(key)){
				//新用户占比
				List<Map<String, Object>> startUprows=radioLine.get("newclientRaterows");
		    	resultMap.put("newclientRaterows",getRadioLine(today,contrastday,"NEW_CLIENT_RATE",startUprows));
				
			}
    	} 
    	
//    	System.err.println(resultMap);
    	return resultMap;
    }
    
    /**
     * 新增用户 点击渠道时触发
     * @param response
     * @param today
     * @param contrastday
     * @param radioname
     * @return
     */
    @RequestMapping("/newutmsourceclick")
    @ResponseBody
    public Map<String,Object>  newutmsourceclick(HttpServletResponse response,
			@RequestParam(value = "today", required = false) String today,
			@RequestParam(value = "contrastday", required = false) String contrastday,
			@RequestParam(value = "utmsource", required = false) String utmsource) {
//    	utmsource = "xiaomi";
    	Map<String, Object> resultMap = Maps.newHashMap();
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	response.setCharacterEncoding("UTF-8");
    	response.setContentType("text/html;charset=UTF-8");
    	
    	// 参数异常处理
    	if (StringUtils.isBlank(today))
    		today = DvDateUtils.getDate(new Date(), 0);
    	if (StringUtils.isBlank(contrastday))
    		contrastday = DvDateUtils.getDate(new Date(), -7);
    	/**
    	 * 新增 渠道安装量  折现图
    	 */
    	
    	if(StringUtils.isEmpty(utmsource)){
    		List<Map<String, Object>> utmSourceInstall = mModel.getUtmSourceInstall(PLATFORM,today,contrastday);
    		resultMap.put("newutmSourceInstall",getRadioLine(today,contrastday,"install_number",utmSourceInstall));
    	}else{
    		List<Map<String, Object>> utmSourceInstallClick = mModel.getUtmSourceInstallClick(PLATFORM,utmsource,today,contrastday);
    		resultMap.put("newutmSourceInstall",getRadioLine(today,contrastday,"install_number",utmSourceInstallClick));
    	}
    	
    	return resultMap;
    	
    }
    /**
     * 活跃用户  点击渠道时触发
     * @param response
     * @param today
     * @param contrastday
     * @param radioname
     * @return
     */
    @RequestMapping("/utmsourceclick")
    @ResponseBody
    public Map<String,Object>  utmsourceclick(HttpServletResponse response,
    		@RequestParam(value = "today", required = false) String today,
    		@RequestParam(value = "contrastday", required = false) String contrastday,
    		@RequestParam(value = "utmsource", required = false) String utmsource) {
//    	utmsource = "xiaomi";
    	Map<String, Object> resultMap = Maps.newHashMap();
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	response.setCharacterEncoding("UTF-8");
    	response.setContentType("text/html;charset=UTF-8");
    	
    	// 参数异常处理
    	if (StringUtils.isBlank(today))
    		today = DvDateUtils.getDate(new Date(), 0);
    	if (StringUtils.isBlank(contrastday))
    		contrastday = DvDateUtils.getDate(new Date(), -7);
    	
    	if(StringUtils.isEmpty(utmsource)){
        	/**
        	 * 活跃 用户数
        	 */
        	List<Map<String, Object>> utmSourceClientNumber = mModel.getAppUtmSourceClientNumber(PLATFORM,null,today,contrastday);
        	resultMap.put("utmSourceClientNumber",getRadioLine(today,contrastday,"client_number",utmSourceClientNumber));
        	
        	/**
        	 * 活跃 打开次数
        	 */
        	List<Map<String, Object>> utmSourceOpenNumber = mModel.getAppUtmSourceOpenNumber(PLATFORM,null,today,contrastday);
        	resultMap.put("utmSourceOpenNumber",getRadioLine(today,contrastday,"open_number",utmSourceOpenNumber));
    	}else{
    		
    		List<Map<String, Object>> utmSourceClientNumberClick = mModel.getAppUtmSourceClientNumber(PLATFORM,utmsource,today,contrastday);
    		resultMap.put("utmSourceClientNumber",getRadioLine(today,contrastday,"client_number",utmSourceClientNumberClick));
    		
    		
    		List<Map<String, Object>> getAppUtmSourceOpenNumberClick = mModel.getAppUtmSourceOpenNumber(PLATFORM,utmsource,today,contrastday);
    		resultMap.put("utmSourceOpenNumber",getRadioLine(today,contrastday,"open_number",getAppUtmSourceOpenNumberClick));
    	}
    	
    	return resultMap;
    	
    }
    
    
    
    /**
     * 格式化折现图数据
     * @param today
     * @param contrastday
     * @param column
     * @param radioLine
     * @return
     */
	public Map<String,Object> getRadioLine(String today,String contrastday,String column,List<Map<String, Object>> radioLine ){
    	Map<String,Object> startUPitemMap = Maps.newHashMap();
    	startUPitemMap.put("legend",DVConstants.REALTIME_CHART_LEGEND);
    	startUPitemMap.put("xAxis", DVConstants.TWENTY_FOUR_HOURS_XAXIS);
    	Object[] radioToday = new Object[24];
    	Object[] radioContrastday = new Object[24];
    	 for(int i=0;i<radioLine.size();i++){
    		 Map<String,Object> map = radioLine.get(i);
             String day = String.valueOf(map.get("DATADATE"));
             Integer hour = Integer.parseInt(String.valueOf(map.get("T_HOUR")));
             if(today.equals(day)){
            	 radioToday[hour]=map.get(column);
             }else if(contrastday.equals(day)){
            	 radioContrastday[hour]=map.get(column);
             }
    	 }
    	startUPitemMap.put("today", radioToday);
        startUPitemMap.put("contrastday", radioContrastday);
        return startUPitemMap;
    }
    
}


















